import im from '../mod/im'
import Log from '../mod/log'

import Consts from '../assets/consts'

Log.init({data: {platform: 'web'}})
let log = new Log('test-im', Log.levelVerbose)

let handler = (what, arg1, arg2, obj) => {
  const cwhat = Consts.im.what
  const carg1 = Consts.im.arg1
  const carg2 = Consts.im.arg2

  switch (what) {
    case cwhat.disconnect:
      log.w('im disconnected', obj)
      break;

    case cwhat.disconnectWillConnect:
      log.w('im disconnect, auto connect', obj)
      break;

    case cwhat.receive: {
      if (arg1 === carg1.success) {

        switch (arg2) {
          case carg2.normal: {
            log.d('receive', obj.session, obj)

            if ('p2p' === obj.scene) {
              // [Neo] .TODO auto read message
              im.message.read(obj)
              .then((m) => {
                log.d('read', m)
              }).catch((e) => {
                log.w('read failed', e)
              })
            }

            im.sessionList()
            .then(e => log.d('list', e))
            .catch(e => log.e(e))
          }
            break;

          case carg2.system: {
            log.v('receive', obj)
          }
            break;

          case carg2.none:
          default:
            break;
        }

      } else {
        log.e('receive failed', arg1, arg2, obj)
      }
    }
      break;

    case cwhat.notify: {
      log.d("notify: " + arg2 + ", ", obj)
    }
      break;

    case cwhat.connected:
      log.v('im connected')
      break;

    case cwhat.synced:
      log.v('im synced')
      break;

    default:
      log.w('im unhandled: ', what, arg1, arg2, obj)
      break;
  }
}

const imOptions = {
  appKey: Consts.imAppKey,
  account: Consts.imToAccount,
  token: Consts.imToToken,

  debug: false,
}

const size = 50
// const sid = 'team-' + Consts.imToTeamId
const sid = 'p2p-' + Consts.imToAccount
const message = {
  type: 'text',
  text: new Date(),

  // type: 'custom',
  // content: Consts.msgContent,
  // custom: Consts.msgCustom,

  // type: 'file',
  // fileType: 'file', // image, audio, video, file
  // blob: Consts.msgBlob,
  // // dataURL: '',
  // // fileInput: domImput,
  // // uploadprogress: onUploadProgress,
  // // uploaddone: onUploadDown,
  // // beforeupload: onBeforeUpload,
  // // beforesend: onBeforeSend,
}

let ts = null
let fetch = _ => {

  im.message.list(sid, ts, size, true)
  .then((r) => {

    if (r.length < size) {
      // [Neo] .TODO fetched all local message
      log.d('fetched all', r)
    } else {

      log.v('fetched ' + size)
      setTimeout(_ => {
        ts = r[size - 1].ts
        fetch()
      }, 100)

    }
  })

}

let test = {
  case: _ => {

    im.init(imOptions, handler)

    .then(l => im.init(Object.assign(imOptions, {
      account: Consts.imAccount,
      token: Consts.imToken
    }), handler))

    // .then(_=> im.api(im.group.quit, {id: 1}))
    // .then(_ => im.message.send('p2p-' + Consts.imAccount2,
    //     {"type": "text", "text": new Date()}))
    // .then(o => im.message.forward('p2p-' + Consts.imToAccount, o))
    // .then(e => log.d(e))
    // .then(im.sessionList)
    .then(e => log.d(e))
    // .then(_ => im.session.delete('p2p-' + Consts.imToAccount))
    // .then(im.sessionList)
    // .then(im.disconnect)
    // .then(_ => im.init(imOptions, handler))
    // .then(im.sessionList)
    // .then(r => log.d(r))
    .catch(e => log.e(e))

    // im.message.send(sid, message)
    // .then(m => {
    //   log.d('send', m)
    //   log.w('check isRead', im.message.isRead(m))
    //
    //   setTimeout(_ => {
    //     log.w('check isRead again', im.message.isRead(m))
    //   }, 3000)
    // })

    // fetch()

  }
}

export default test
